﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GrainManage.Entities;
using GrainManage.Server.Factory;
using System.Reflection;

namespace GrainManage.Server.Business
{
    public class BaseBusiness
    {
        public static void SetResponse(int statusCode, BaseOutputModel output, string message)
        {
            const string flag = "StatusCode:";
            if (statusCode > 1)
            {
                throw new Exception(flag + statusCode);
            }
            else if (System.Text.RegularExpressions.Regex.IsMatch(message, "^StatusCode:[0-9]+$"))
            {
                statusCode = Convert.ToInt32(message.Replace(flag, string.Empty));
            }

            output.Status = new StatusInfo();
            output.Status.StatusCode = statusCode;
            if (statusCode > 1)
            {
                message = ObjectFactory.StatusService.GetMessage(statusCode);
            }
            output.Status.Message = message;
        }

        public static void SetResponse(int statusCode, BaseOutputModel output)
        {
            SetResponse(statusCode, output, string.Empty);
        }

        protected static List<TModel> ConvertToModel<TModel>(DataSet table, OutputModel output) where TModel : class,new()
        {
            List<TModel> result = new List<TModel>();
            if (table != null && table.Tables.Count > 0)
            {
                result = GrainManage.Data.Util.Convertor.ConvertToEntity<TModel>(table.Tables[0]);
            }

            if (output != null)
            {
                output.Total = GetTotalRow(table);
            }
            return result;
        }

        protected static TModel ConvertToModel<TModel>(DataRow row) where TModel : class,new()
        {
            TModel result = default(TModel);
            if (row != null)
            {
                result = GrainManage.Data.Util.Convertor.ConvertToEntity<TModel>(row);
            }
            return result;
        }

        protected static int GetTotalRow(DataSet table)
        {
            int totalRow = 0;
            if (table != null && table.Tables != null && table.Tables.Count == 2)
            {
                totalRow = Convert.ToInt32(table.Tables[1].Rows[0][0]);
            }
            return totalRow;
        }

        protected static void WriteLog<InputParameter, OutputParameter>(System.Reflection.MethodBase method, Exception e, InputParameter input, OutputParameter output)
        {
            if (LogUtil.CanWrite(LogLevel.Error))
            {
                var message = LogUtil.GetMessage(method, e, input, output, "POST");
                LogUtil.Write(message, LogLevel.Error);
            }
        }

        public static TResultModel CopyTo<TResultModel, TOriginalModel>(TOriginalModel original)
            where TResultModel : class,new()
            where TOriginalModel : class,new()
        {
            return GrainManage.Data.Util.Convertor.CopyTo<TResultModel, TOriginalModel>(original);
        }

        public static List<TResultModel> CopyTo<TResultModel, TOriginalModel>(List<TOriginalModel> originalModels)
            where TResultModel : class,new()
            where TOriginalModel : class,new()
        {
            return GrainManage.Data.Util.Convertor.CopyTo<TResultModel, TOriginalModel>(originalModels);
        }

    }
}
